package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void addSetmealDish(SetmealDto setmealDto) {
        //1、保存套餐数据到 setmeal表中
        this.save(setmealDto);

        //2、保存套餐菜品数据到 setmeal_dish表中
        //2.1 获取当前套餐的菜品列表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //2.2 循环遍历，获取每一个套餐菜品数据 setmealDish
        for (SetmealDish setmealDish : setmealDishes) {
            //2.3 获取套餐id，并set到setmealDish中
            Long id = setmealDto.getId();
            setmealDish.setSetmealId(id);
            //Long setmealId = setmealDish.getSetmealId();
            //setmealDish.setSetmealId(setmealId);
            //2.4 保存套餐菜品数据 setmealDish
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Transactional
    @Override
    public void updateSetmealService(SetmealDto setmealDto) {
        //1、根据id更新 setmeal 套餐表数据 update setmeal set xx=?,.. where id =?
        // 获取套餐id   setmealDto.getId()
        Long setmealId = setmealDto.getId();
        LambdaUpdateWrapper<Setmeal> setmealLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        setmealLambdaUpdateWrapper.eq(Setmeal::getId,setmealId);
        this.update(setmealDto,setmealLambdaUpdateWrapper);

        //2、操作套餐对应的菜品数据： setmeal_dish
        //2.1 先根据套餐id删除套餐对应的菜品数据： delete from setmeal_dish where setmeal_id =?


        // 使用 setmealDishMapper 根据套餐id进行删除 ：delete from setmeal_dish where setmeal_id =?
        setmealDishMapper.deleteById(setmealId);

        //2.2 重新添加新的套餐对应的菜品数据 insert setmeal_dish values (..)
        // setmealDto.getSetmealDishes() 获取套餐的菜品集合 List<SetmealDish>
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();

        // 遍历套餐菜品集合数据
        for (SetmealDish dish : dishes) {
            dish.setSetmealId(setmealId);
            LambdaUpdateWrapper<SetmealDish> setmealDishLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            setmealDishLambdaUpdateWrapper.eq(SetmealDish::getSetmealId,setmealId);
            setmealDishMapper.update(dish,setmealDishLambdaUpdateWrapper);
        }
        // 有没有数据需要set ： 设置套餐id  setSetmealId

        // 添加套餐菜品数据 insert
    }
}
